﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Xml.Linq;
//using Microsoft.AspNet.SignalR;
//using Real_Time_Map_TechBrij.Hubs;
using System.Data;
using GPS.COMMON;
using GPS.DataAccessLayer;
using GPS.MODELS;

namespace GPS.Hubs
{
    public class GPS_OnlineMap_Data
    {
        //private static FileSystemWatcher fWatcher;
        //private static String xmlFilePath = HttpContext.Current.Server.MapPath("~/App_Data/location.xml");
        private static List<GPS_TRACKING_Location> objLoc = new List<GPS_TRACKING_Location>();


        public static IEnumerable<GPS_TRACKING_Location> Data
        {
            get
            {
                return objLoc.AsReadOnly();
            }
        }

        /// <summary>
        /// One Time Initialization
        /// </summary>
        public static void init()
        {
            Initialize();
            ReadNewLines();
        }


        /// <summary>
        /// To setup FileSystemWatcher
        /// </summary>
        private static void Initialize()
        {
            //fWatcher = new FileSystemWatcher();
            //fWatcher.Path = Path.GetDirectoryName(xmlFilePath);
            //fWatcher.Filter = Path.GetFileName(xmlFilePath);
         //   fWatcher.Changed += watcher_Changed;
            //fWatcher.EnableRaisingEvents = true;
            //fWatcher.Error += OnError;
        }

        private static void OnError(object sender, ErrorEventArgs e)
        {
            //fWatcher.Dispose();
            Initialize();
        }


        //private static void watcher_Changed(object sender, FileSystemEventArgs e)
        //{
        //    try
        //    {
        //        fWatcher.EnableRaisingEvents = false;
        //        var newLoc = ReadNewLines();
        //        //Pass new points to client
        //        var context = GlobalHost.ConnectionManager.GetHubContext<GPS_OnlineMaps_Hub>();
        //        context.Clients.All.addLocations(newLoc);
        //    }
        //    finally
        //    {
        //        fWatcher.EnableRaisingEvents = true;
        //    }

        //}

        /// <summary>
        /// To read new lines 
        /// </summary>
        public static IEnumerable<GPS_TRACKING_Location> ReadNewLines()
        {
            //XDocument xmlDoc;
            //using (Stream s = File.OpenRead(xmlFilePath))
            //{
            //    xmlDoc = XDocument.Load(s);
            //}
            //int total = objLoc.Count();
            //var newLoc = (from item in xmlDoc.Descendants("location").Select((x, index) => new { x, index })
            //              where item.index >= total
            //              select new GPS_TRACKING_Location { Name = (String)item.x.Attribute("name"), Lat = (double)item.x.Attribute("lat"), Lng = (double)item.x.Attribute("lng") });
            //objLoc.AddRange(newLoc);
            //return newLoc;
            List<GPS_TRACKING_Location> lstCate = null;
            DataSet ds = SqlHelper.ExecuteDataset(GPS_SqlConnectHelper.ConnectionString(), CommandType.Text, "select name Name, lat Lat, lng Lng from GPS_TRACKING_Location");
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                lstCate = GPS_Library.MapDataTableToList<GPS_TRACKING_Location>(ds.Tables[0]);
            var newLoc = lstCate.AsEnumerable<GPS_TRACKING_Location>();
            objLoc.AddRange(newLoc);
            return newLoc;
        }


        /// <summary>
        /// To read new lines 
        /// </summary>
        public static IEnumerable<GPS_TRACKING_Location> GetNewLinesFromDBSQL()
        {
            List<GPS_TRACKING_Location> lstCate = null;
            DataSet ds = SqlHelper.ExecuteDataset(GPS_SqlConnectHelper.ConnectionString(), CommandType.Text, "select name Name, lat Lat, lng Lng from GPS_TRACKING_Location");
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                lstCate = GPS_Library.MapDataTableToList<GPS_TRACKING_Location>(ds.Tables[0]);
            return lstCate.AsEnumerable<GPS_TRACKING_Location>();
        }
    }
}